package com.krtech.wecard.module.mayi.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.krtech.wecard.module.mayi.MayiPayEnums;
import com.krtech.wecard.module.mayi.entity.MayiMachineManage;
import com.krtech.wecard.module.mayi.entity.MayiPayList;
import com.krtech.wecard.module.mayi.service.MayiMachineManageService;
import com.krtech.wecard.module.mayi.service.MayiPayListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class MayiHeartbeatTask {

    @Autowired
    private MayiMachineManageService machineManageService;

    @Autowired
    private MayiPayListService mayiPayListService;

    @Scheduled(cron = "0/30 * * * * ? ")
    public void userTask(){
//        log.info("蚂蚁心跳定时任务:"+ DateUtil.now());

        //定时清楚故障掉，machineId为null的情况
        machineManageService.remove(new QueryWrapper<MayiMachineManage>().isNull(MayiMachineManage.COL_MACHINE_ID));

        //直接判断上次的心跳时间是否超过了3分
        List<MayiMachineManage> machineList = machineManageService
                .list();
        for (MayiMachineManage machine:machineList){
            //判断上次存活
            DateTime now = DateUtil.date();

            long between = DateUtil.between(machine.getLastConnectTime(), now, DateUnit.MINUTE);
            //超时三分钟
            if (between > 3){
                //设置为故障状态
                machine.setStatus(0);
                machineManageService.updateById(machine);
            }
        }




    }
}
